package com.chidopi.cdf.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;

public class DynaDailyRollingAppender extends DailyRollingFileAppender {
	private Logger logger1 = Logger.getLogger(this.getClass().getName());
	private static String oldFileName = null;
	private static String oldPath = null;
	public void generateLogFile(LocationInfo info) {		
		if (oldPath == null) {
			oldPath = this.fileName.substring(0, this.fileName.lastIndexOf("test"));
		}
		SimpleDateFormat sdf = new SimpleDateFormat(this.getDatePattern());
		String fileName1 = oldPath;
		fileName1 += info.getClassName() + sdf.format(Calendar.getInstance().getTime());
		this.logger1.info(fileName1 + "; " + this.fileName);
		
		oldFileName = fileName1;
		this.setFile(fileName1);	
		this.activateOptions();
	}
	
	@Override
	protected void subAppend(LoggingEvent event) {
		String className =  event.getLocationInformation().getClassName();
		this.logger1.info("Class:" + className);
		if (oldFileName != className) {
			this.generateLogFile(event.getLocationInformation());
		}
		super.subAppend(event);
	}
}
